/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.ctrl.action;

import de.uhilger.servicehub.ServiceHub;
import de.uhilger.servicehub.view.AppPanel;
import de.uhilger.servicehub.view.ServerPanel;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.logging.Level;
import javax.swing.JFileChooser;

/**
 * An Action to select a file from the local file system.
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class FileSelectAction extends AbstractServiceHubAction {
  
  /**
   * Create a new object of class FileSelectAction
   * @param owner the ServiceHub application that owns this action
   */
  public FileSelectAction(ServiceHub owner) {
    super(owner);
  }

  /**
   * Display a file chooser to select a file in the
   * local file system and report the selected file
   * to all ActionResultListeners
   *
   * @param e ActionEvent  the event that fired this action
   */
  public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    ServiceHub.log("actionCommand " + e.getActionCommand(), getClass(), Level.FINEST);
    if(e.getActionCommand().equals(ServerPanel.AC_SERVER_PANEL_WEB_DIR_SELECT)) {
      chooser.setSelectedFile(new File(owner.getServer().getBase()));
    } else if(e.getActionCommand().equals(AppPanel.AC_APP_PANEL_FILE_SEL_BTN)) {
      chooser.setSelectedFile(new File(AppPanel.getDeployFileName()));
    }
    int returnVal = chooser.showOpenDialog(ServiceHub.findMainFrame());
    if(returnVal == JFileChooser.APPROVE_OPTION) {
      File file = chooser.getSelectedFile();
      fireActionResult(e, file);
    }
  }

}
